package com.mastercard.assets.web.struts.actions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.flows.FlowUtil;
import com.mastercard.assets.flows.process.FlowProcessContext;
import com.mastercard.assets.flows.process.FlowProcessResult;
import com.mastercard.assets.flows.to.FlowDataTO;
import com.mastercard.assets.jpa.TblTareaPreimplementacion;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.FlowProcessForm;
import com.mastercard.assets.web.util.to.UserLogin;

public class FlowProcessIn
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		FlowProcessForm f = ( FlowProcessForm ) form ;
		
		int cveTareaPreimplementacion = f.getCveWorkPreimplementation( ) ;
		String exit = f.getExit( ) ;
		int operation = f.getOperation( ) ;
		
		System.out.println( "Clave de salida: " + exit ) ;
		System.out.println( "Clave de tarea preimplementacion: " + cveTareaPreimplementacion ) ;
		System.out.println( "Validacion: " + ( exit == null ) ) ;
		
		if( operation == 1 )
			return mapping.findForward( "CancelWorkExecution" ) ;
		
		// toma el registro de tbl_tarea_preimplementacion
		TblTareaPreimplementacion tblTareaPreimplementacion = MasterCardDAO.findTareaPreimplementacionById( cveTareaPreimplementacion ) ;
		
		// crea el contexto
		FlowProcessContext ctx = new FlowProcessContext( request , f , cveTareaPreimplementacion , user ) ;
		
		// en caso de que el nombre de la salida sea null o vacio
		if( exit == null || exit.trim( ).length( ) == 0 )
		{
			System.out.println( "Viene sin salida..." ) ;
			try
			{
				FlowDataTO to = FlowUtil.preProcessForm( ctx ) ;
				
				System.out.println( "FlowProcessIn.java: data/" + to ) ;
				
				request.setAttribute( "FlowData" , to ) ;
			}catch( Exception e )
			{
				e.printStackTrace( ) ;
				
				user.setMessageError( e.getMessage( ) ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "error" ) ;
			}
		}
		// en caso de que venga con salida
		else
		{
			System.out.println( "Viene con salida... " + exit ) ;
			try
			{
				// procesa los datos
				FlowProcessResult result = FlowUtil.postProcessForm( ctx ) ;
				
				System.out.println( "FlowProcessIn.java: result.getResult/" + result.getResult( ) ) ;
				System.out.println( "FlowProcessIn.java: result.getResult/" + ( result.getResult( ) == FlowUtil.FLOW_CONSTANTS_FORM_PROCESS_DATA_PROCESS_ERROR_RELOAD ) ) ;
				System.out.println( "FlowProcessIn.java: result.getException/" + ( result.getException( ) == null ? "" : result.getException( ).getMessage( ) ) ) ;

				// en caso de que sea recargar
				if( result.getResult( ) == FlowUtil.FLOW_CONSTANTS_FORM_PROCESS_DATA_PROCESS_ERROR_RELOAD )
				{
					System.out.println( "FlowProcessIn.java: result.getError/" + result.getMessage( ) ) ;
					user.setMessageError( result.getMessage( ) ) ;
					
					System.out.println( "FlowProcessIn.java: coloca en request/" + result ) ;
					
                                        MasterCardDAO.closeEntityManager( ) ;
				  	return mapping.findForward( "AfterProcessError" ) ;
				}
			}catch( MasterCardException e )
			{
                            MasterCardDAO.closeEntityManager( ) ;
				e.printStackTrace( ) ;
				
				user.setMessageError( e.getSingularMessage( ) ) ;
				
				return mapping.findForward( "AfterProcessError" ) ;
			}catch( Exception e )
			{
				e.printStackTrace( ) ;
				
				user.setMessageError( e.getMessage( ) ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "AfterProcessError" ) ;
			}
			
                        MasterCardDAO.closeEntityManager( ) ;
		  	return mapping.findForward( "AfterProcess" ) ;
		}
		
                MasterCardDAO.closeEntityManager( ) ;
	  	return mapping.findForward( "success" ) ;
	}
	
}
